<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="县区" prop="city" v-show="cityShow">
        <el-select
          v-model="queryParams.city"
          clearable
          placeholder="请选择"
          size="small"
          @change="selectCity"
        >
          <el-option
            v-for="item in this.cityData"
            :key="item.deptCode"
            :label="item.deptName"
            :value="item.deptCode">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="乡镇" prop="town" v-show="townShow">
        <el-select
          v-model="queryParams.town"
          clearable
          placeholder="请选择"
          size="small"
          @change="selectTown"
        >
          <el-option
            v-for="item in this.townData"
            :key="item.deptCode"
            :label="item.deptName"
            :value="item.deptCode">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="村" prop="village" v-show="villageShow">
        <el-select
          v-model="queryParams.village"
          clearable
          placeholder="请选择"
          size="small"
          @change="selectVillage"
        >
          <el-option
            v-for="item in this.villageData"
            :key="item.deptCode"
            :label="item.deptName"
            :value="item.deptCode">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="承包方名称" prop="cbfmc">
        <el-input
          v-model="queryParams.cbfmc"
          placeholder="请输入承包方名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退耕年份" prop="tyear">
        <el-date-picker
          v-model="queryParams.tyear"
          value-format="yyyy"
          type="year"
          placeholder="选择退耕年份">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!--<el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            :disabled="multiple"
            @click="handleUpdateCbf"
            v-hasPermi="['supervise:supervise:add']"
          >变更承包方
          </el-button>
        </el-col>-->
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ownership:cbf:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="CBFList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column width="220" label="行政区划" align="center" prop="county">
        <template slot-scope="scope">
          {{scope.row.county}} - {{scope.row.township}} - {{scope.row.village}}
        </template>
      </el-table-column>
      <el-table-column label="承包方编码" align="center" prop="cbfbm"/>
     <!-- <el-table-column label="县区" align="center" prop="county"/>
      <el-table-column label="乡镇" align="center" prop="township"/>
      <el-table-column label="村" align="center" prop="village"/>-->
      <el-table-column label="承包方名称" align="center" prop="cbfmc"/>
      <el-table-column label="退耕年份" align="center" prop="tyear"/>
      <el-table-column label="证件号码" align="center" prop="cbfzjhm"/>
      <el-table-column label="联系电话" align="center" prop="lxdh"/>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ownership:cbf:remove']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateCbf(scope.row)"
            v-hasPermi="['ownership:cbf:remove']"
          >变更承包方
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDk(scope.row)"
            v-hasPermi="['ownership:cbf:query']"
          >
            <svg-icon class="svg" icon-class="land"/>
            地块信息
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ownership:cbf:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="承包方编码" prop="cbfbm">
          <el-input v-model="form.cbfbm" disabled/>
        </el-form-item>
        <el-form-item label="承包方名称" prop="cbfmc">
          <el-input v-model="form.cbfmc" placeholder="请输入承包方名称"/>
        </el-form-item>
        <el-form-item label="证件号码" prop="cbfzjhm">
          <el-input v-model="form.cbfzjhm" placeholder="请输入证件号码"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="lxdh">
          <el-input v-model="form.lxdh" placeholder="请输入联系电话"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="openCbf" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="承包方编码" prop="cbfbm">
          <el-input v-model="form.cbfbm" disabled/>
        </el-form-item>
        <el-form-item label="承包方名称" prop="cbfmc">
          <el-input v-model="form.cbfmc" placeholder="请输入承包方名称"/>
        </el-form-item>
        <el-form-item label="证件号码" prop="cbfzjhm">
          <el-input v-model="form.cbfzjhm" disabled placeholder="请输入证件号码"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="lxdh">
          <el-input v-model="form.lxdh" placeholder="请输入联系电话"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="openDetails" width="800px" append-to-body>
      <div style="min-height: 400px">
        <el-table v-loading="loading" :data="dkList">
          <el-table-column label="地块编码" align="center" width="180" prop="dkbm"/>
          <el-table-column label="面积（亩）" align="center" prop="scmjm"/>
          <el-table-column label="土地性质" align="center" prop="dksx"/>
          <el-table-column label="东至" align="center" prop="dz"/>
          <el-table-column label="南至" align="center" prop="nz"/>
          <el-table-column label="西至" align="center" prop="xz"/>
          <el-table-column label="北至" align="center" prop="bz"/>
          <el-table-column label="地块名称" align="center" prop="dkmc"/>
        </el-table>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addCBF, delCBF, exportCBF, getCBF, listCBF, updateCBF, updateCbfName} from "@/api/chgl/cbf";
import {childlistUse, listDept} from "@/api/system/dept";
import Template from "@/views/message/template/index";
import {listDK} from "@/api/chgl/dk";
import {getInfo} from "@/api/login";

export default {
  name: "CBF",
  components: {Template},
  data() {
    return {
      // 遮罩层selectVillage
      loading: false,
      // 选中数组
      ids: [],
      dkList: [],
      city: '',
      town: '',
      village: '',
      cityShow: '',
      townShow: '',
      villageShow: '',
      cityData: [],
      townData: [],
      villageData: [],
      deptList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      CBFList: [{
        cbfbm: 321323123123,
        cbfmc: '张三',
        cbfzjhm: '2',
        lxdh: '13888888888',
        cbfdz: '宿迁',
        dz: '路',
        nz: '河',
        xz: '李斯',
        bz: '路',
      }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openCbf: false,
      cbfId: '',
      openDetails: false,
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cbflx: null,
        cbfbm: null,
        cbfmc: null,
        cbfzjlx: null,
        cbfzjhm: null,
        cbfdz: null,
        yzbm: null,
        lxdh: null,
        cbfdcrq: null,
        cbfdcy: null,
        cbfdcjs: null,
        gsjs: null,
        gsjsr: null,
        gsshrq: null,
        gsshr: null,
        qsjd: null,
        ykt: null
      },
      querydkParams: {
        pageNum: 1,
        pageSize: 50,
        cbfbm: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cbfmc: [
          {required: true, message: "承包方名称不能为空", trigger: "blur"}
        ],
        lxdh: [
          {
            pattern: /(\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$/,
            message: "请输入正确的联系电话",
            trigger: "blur"
          }
        ],
        cbfzjhm: [
          {
            pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
            message: "请输入正确的证件号码",
            trigger: "blur"
          }
        ],
      }
    };
  },
  created() {
    this.getDicts('sys_normal_disable').then(response => {
      this.statusOptions = response.data
    });

    listDept(this.queryParams).then(response => {
      this.deptList = response.data;
    });
    this.getList();
    getInfo().then(response => {
      let deptCode = response.user.dept.deptCode;

      if (deptCode.length == 4) {//新区
        this.cityShow = true;
        this.townShow = true;
        this.villageShow = true;
        childlistUse(deptCode).then(response => {
          this.cityData = response.data;
        });
      } else if (deptCode.length == 6) {//县
        this.cityShow = false;
        this.townShow = true;
        this.villageShow = true;
        childlistUse(deptCode).then(response => {
          this.townData = response.data;
        });
      } else if (deptCode.length == 9) {//镇
        this.cityShow = false;
        this.townShow = false;
        this.villageShow = true;
        childlistUse(deptCode).then(response => {
          this.villageData = response.data;
        });
      } else if (deptCode.length == 12) {//村
        this.cityShow = false;
        this.townShow = false;
        this.villageShow = false;
      } else {
        this.childlist(1306);
        this.cityShow = true;
        this.townShow = true;
        this.villageShow = true;
      }
    });
  },
  methods: {
    selectDeptLabel(datas, value) {
      var actions = [];
      Object.keys(datas).some((key) => {
        if (datas[key].dictValue == ('' + value)) {
          actions.push(datas[key].dictLabel);
          return true;
        }
      })
      return actions.join('');
    },
    // 字典状态字典翻译
    cityFormat(city) {
      return this.selectDeptLabel(this.deptList, city);
    },
    // 字典状态字典翻译
    townFormat(row, column) {
      return this.selectDeptLabel(this.deptList, row.cbfbm.substring(0, 9));
    },
    // 字典状态字典翻译
    villageFormat(row, column) {
      return this.selectDeptLabel(this.deptList, row.cbfbm.substring(0, 12));
    },
    childlist(deptCode) {
      childlistUse(deptCode).then(response => {
        this.cityData = response.data;
      });
    },
    // 选择县
    selectCity(val) {
      this.city = val;
      this.queryParams.cbfbm = val;
      if(val!=""&&val!=null) {
        childlistUse(val).then(response => {
          this.townData = response.data;
        });
      }
    },
    // 选择镇
    selectTown(val) {
      this.town = val;
      this.queryParams.cbfbm = val;
      if(val!=""&&val!=null) {
        childlistUse(val).then(response => {
          this.villageData = response.data;
        });
      }
    },
    // 选择村
    selectVillage(val) {
      this.queryParams.cbfbm = val;
      this.village = val;
    },
    getList() {
      this.loading = true;
      listCBF(this.queryParams).then(response => {
        this.CBFList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openCbf = false;
      this.openDetails = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cbfId: null,
        cbfbm: null,
        cbflx: null,
        cbfmc: null,
        cbfzjlx: null,
        cbfzjhm: null,
        cbfdz: null,
        yzbm: null,
        lxdh: null,
        cbfdcrq: null,
        cbfdcy: null,
        cbfdcjs: null,
        gsjs: null,
        gsjsr: null,
        gsshrq: null,
        gsshr: null,
        qsjd: null,
        ykt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.cbfbm = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cbfId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cbfId = row.cbfId || this.ids
      this.cbfId=cbfId;
      this.open = true;
      this.title = "修改";
      getCBF(cbfId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 修改按钮操作 */
    handleUpdateCbf(row) {
      this.reset();
      const cbfId = row.cbfId || this.ids
      this.cbfId=cbfId;
      this.title = "变更承包方";
      getCBF(cbfId).then(response => {
        this.form = response.data;
        this.openCbf = true;
        this.title = "修改";
      });
    },
    handleDk(row) {
      this.reset();
      const cbfbm = row.cbfbm || this.ids
      this.openDetails = true;
      this.title = "地块信息";
      this.querydkParams.cbfbm = cbfbm;
      listDK(this.querydkParams).then(response => {
        this.dkList = response.rows;
        this.loading = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      let cbfId= this.cbfId;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cbfbm != null) {
            this.form.cbfId=cbfId;
            updateCBF(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCBF(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    updateForm() {
      let cbfId= this.cbfId;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cbfbm != null) {
            updateCbfName({cbfId:cbfId,cbfmc:this.form.cbfmc}).then(response => {
              this.msgSuccess("修改成功");
              this.openCbf = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let cbfbms =row.cbfId|| this.ids;
      this.$confirm('是否确认删除数据项?', "警告", {
        cancelButtonClass: 'btn-custom-cancel',
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delCBF(cbfbms);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有数据项?', "警告", {
        cancelButtonClass: 'btn-custom-cancel',
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportCBF(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(() => {
      })
    }
  }
}
;
</script>
